package com.jye.rseven.user.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jye.rseven.domain.Courses;
import com.jye.rseven.domain.User;
import com.jye.rseven.service.CoursesService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/courses")
@Api(tags = "课程控制器API")
public class CoursesController {

    @Reference
    CoursesService coursesService;


    /*
  * 通过id查找课程
  * GetMapping为方便测试暂时不用
  * */
    @GetMapping(value = "/findCourseByIdAndUId")
    @ApiOperation(value = "查找课程", notes = "根据课程id与用户id(用来判断是否点赞购买等)查找课程", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "课程ID", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findCourseByIdAndUId(int id,int uId) {
        JsonResult jsonResult = new JsonResult();
        Courses c = coursesService.findCourseByIdAndUId(id,uId);
        if (!StringUtils.isEmpty(c)) {
            jsonResult.setData(c);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
    * 查找课程
    * */
    @GetMapping("/findAllCourses")
    @ApiOperation(value = "查找所有课程", notes = "查找所有课程", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findAllCourses(int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Courses> list = coursesService.findAllCourses(skip, limit);
        if (list != null) {
            jsonResult.setData(list);

        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }


    /*
   * 查找课程
   * */
    @GetMapping("/findTeacherCoursesByUId")
    @ApiOperation(value = "老师查找自己课程", notes = "根据id查询自己为老师的课程", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findTeacherCoursesByUId(int uId) {
        JsonResult jsonResult = new JsonResult();
        List<Courses> coursesList = coursesService.findTeacherCoursesByUId(uId);
        if (coursesList.size()!=0) {
            jsonResult.setData(coursesList);

        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("抱歉!没有找到你是老师的课程");
        }
        return jsonResult;
    }


    @GetMapping("/findHotCourses")
    @ApiOperation(value = "查找热门课程", notes = "查找热门课程，目前为10个", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户Id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findHotCourses(int uId,int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Courses> list = coursesService.findHotCourses(uId,skip, limit);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @GetMapping("/findNewestCourses")
    @ApiOperation(value = "查找最新课程", notes = "查找最新课程，目前为10个", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findNewestCourses(int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Courses> list = coursesService.findNewestCourses(skip, limit);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }


    @GetMapping(value = "/findMyCoursesByUId")
    @ApiOperation(value = "查找我的课程", notes = "查找我的课程", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyCoursesByUserId(int uId,int skip,int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Courses> list = coursesService.findMyCoursesByUId(uId,skip,limit);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }


    @GetMapping(value = "/findCoursesByType")
    @ApiOperation(value = "查找分类课程", notes = "根据type查找分类课程", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type",
                    value = "类别", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findCoursesByType(String type,int skip,int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Courses> list = coursesService.findCoursesByType(type,skip,limit);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @PostMapping(value = "/addCourse")
    @ApiOperation(value = "增加课程", notes = "添加课程", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "courses",
                    value = "课程实体", required = true,
                    dataType = "com.jye.rseven.user.domain.Courses"),
    })
    public JsonResult addCourse(Courses courses) {
        JsonResult jsonResult;
        int i = coursesService.addCourse(courses);
        if (i == 1) {
            jsonResult = new JsonResult("添加成功!");

        } else {
            jsonResult = new JsonResult("添加失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @PostMapping(value = "/updateCourse")
    @ApiOperation(value = "修改课程", notes = "修改课程", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "courses",
                    value = "课程实体", required = true,
                    dataType = "com.jye.rseven.user.domain.Courses"),
    })
    public JsonResult updateCourse(Courses courses) {
        JsonResult jsonResult = new JsonResult();
        int i = coursesService.updateCourse(courses);
        if (i == 1) {
            jsonResult = new JsonResult("添加成功!");

        } else {
            jsonResult = new JsonResult("添加失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    @GetMapping("/findCourseUserDetailByCId")
    @ApiOperation(value = "查找该课程下的用户和他们分班情况", notes = "根据cId查找该课程下的用户和他们分班情况", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "课程id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findCourseUserDetailByCId(int cId) {
        JsonResult jsonResult = new JsonResult();
        List<User> userList = coursesService.findCourseUserDetailByCId(cId);
        if(userList.size()!=0){
            jsonResult.setData(userList);
        }else {
            jsonResult.setMsg("该班级无用户!");
        }
        return jsonResult;
    }


    @GetMapping("/findCoursesNotClassUserById")
    @ApiOperation(value = "查找该课程下未分班的用户", notes = "根据cId查找该课程下未分班的用户", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "课程id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findCoursesNotClassUserById(int id ){
        JsonResult jsonResult = new JsonResult();
        List<User> userList = coursesService.findCoursesNotClassUserById(id);
        if(userList.size()!=0){
            jsonResult.setData(userList);
        }else {
            jsonResult.setMsg("该班级无未分班用户!");
        }
        return jsonResult;
    }

    @GetMapping("/findCoursesByName")
    @ApiOperation(value = "模糊查找课程", notes = "根据name模糊查找课程", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name",
                    value = "模糊查询关键字", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findCoursesByName(String name ){
        JsonResult jsonResult = new JsonResult();
        List<Courses> coursesList = coursesService.findCoursesByName(name);
        if(coursesList.size()!=0){
            jsonResult.setData(coursesList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("赞无关键字为"+name+"的课程!");
        }
        return jsonResult;
    }



}
